package com.nea.nehe.lesson02;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Tex2Duv
{
	protected float[] vf =
	{ 0.0f, 0.0f, 800.0f, 0.0f, 0.0f, 480.0f, 480.0f, 800.0f, };
	protected float[] ufv =
	{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, };

	private FloatBuffer vertexBuffer;
	private FloatBuffer textureBuffer;
    public int TextureIndex = 0;
	public Tex2Duv()
	{
		//
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(vf.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.put(vf);
		vertexBuffer.position(0);

		byteBuf = ByteBuffer.allocateDirect(ufv.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuf.asFloatBuffer();
		textureBuffer.put(ufv);
		textureBuffer.position(0);
	}
	
	
	public void draw(GL10 gl) {
		//Bind our only previously generated texture in this case
		gl.glBindTexture(GL10.GL_TEXTURE_2D,TextureIndex);
		
		//Point to our buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		//Set the face rotation
		gl.glFrontFace(GL10.GL_CCW);
		
		//Enable the vertex and texture state
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		
		//Draw the vertices as triangles, based on the Index Buffer information
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vf.length / 3);
		
		//Disable the client state before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		
	}
	
	

}
